import { wxLogin } from '@/config/user/api';
import { userUserStore } from '@/store';
import common from './common';
import storge from './storge.js';
let hospitalMsg = storge.get('hospitalMsg');

export default {
	init(that, callback) {
		
		console.log("login_init: ");
		const tlogin = this;
		if (that && that.userInfo) {
			that.$common.callback(callback, that.userInfo);
			return false
		};
		// #ifdef MP-WEIXIN
			let wxcode = null;
			let hospitalId = that.$store.nowHospital?.id ?? '';
			uni.login({
				provider: 'weixin', //使用微信登录
				success: (res) => {
					console.log(res);
					if (res.code) {
						wxcode = res.code;
						uni.setStorageSync('wxcode', wxcode);
						let first_leader = storge.get('first_leader').value;
						let hospitalCodeId = storge.get('hospitalCodeId').value;
						let paramData = {
							code: wxcode,
							parentId:'',
							hospitalCodeId:'',
							wxMemberInfo:{
								avatarUrl:'',
								gender: 0,
								nickName: "",
								hospitalId
							},
						};
						if (first_leader) {
							paramData.parentId = first_leader
						};
						if(hospitalCodeId){
							paramData.hospitalCodeId = hospitalCodeId
						};
						wxLogin(paramData).then(loginData=>{
							if (loginData.code != 200) {
								that.$common.callback(callback, '');
								return
							};
							uni.setStorageSync('token',loginData.data.token);
							that.$store.commit('setUserInfo', loginData.data.memberInfo);
							if(!hospitalMsg.value){
								that.$store.commit('setNowHospital', {
									hospitalName:loginData.data.memberInfo.hospitalName || '',
									id:loginData.data.memberInfo.hospitalId || '',
									goodsKeyword:[],
									goodsHotKeyword:[]
								});
							};
							that.$common.callback(callback, loginData.data.memberInfo);
						})
						
					}
				},
				fail(err) {
					console.log(err);
				},
			});
			
		// #endif
		// #ifdef APP-PLUS
			let memberInfo = uni.getStorageSync('memberInfo') || '';
			console.log(memberInfo);
			if(!hospitalMsg.value){
				that.$store.commit('setNowHospital', {
					hospitalName:memberInfo.hospitalName || '',
					id:memberInfo.hospitalId || '',
					goodsKeyword:[],
					goodsHotKeyword:[]
					
				});
			};
			that.$store.commit('setUserInfo', memberInfo);  
			that.$common.callback(callback, memberInfo);	
		// #endif
	}

}